import request from "./request";

const http = {
  get(url, params) {
    const config = {
      method: "get",
      url: url
    };
    if (params) config.params = params;
    return new Promise((resolve) => {
      request(config).then((res) => {
        if (res.success == true) {
          resolve(res.data);
        }
      });
    });
  },
  post(url, params) {
    const config = {
      method: "post",
      url: url
    };
    if (params) config.data = params;
    return new Promise((resolve) => {
      request(config).then((res) => {
        if (res.code == 200) {
          resolve(res.data);
        }
      });
    });
  },

  posts(url, params) {
    const config = {
      method: "post",
      url: url
    };
    if (params) config.params = params;
    return new Promise((resolve) => {
      request(config).then((res) => {
        resolve(res);
      });
    });
  },
  getfile(url, params) {
    //新建文件的请求方式
    const config = {
      method: "post",
      url: url,
      //必须设置返回的类型为blob
      responseType: "blob"
    };
    if (params) config.data = params;
    return new Promise((resolve) => {
      request(config).then((res) => {
        if (res.status == 200) {
          if (res.data == null) {
            res.data = {};
          }
          resolve(Object.assign(res.data, res.extra));
        } else {
          //文件流直接返回
          resolve(res);
        }
      });
    });
  },
  put(url, params) {
    const config = {
      method: "post",
      url: url
    };
    if (params) config.params = params;
    return new Promise((resolve) => {
      request(config).then((res) => {
        if (res.success == true) {
          resolve(res.data);
        }
      });
    });
  },
  export(url, params) {
    const config = {
      method: "get",
      url: url,
      responseType: "blob"
    };
    if (params) config.params = params;
    return new Promise((resolve) => {
      request(config).then((res) => {
        resolve(res);
      });
    });
  }
};
export default http;
